<template>
  <div class="tabControlstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <!-- 提示 -->
    <p style="color: #969799; font-size: 12px; margin: 10px 0">拖动选中的导航可对其排序</p>

    <!-- 表单 -->
    <el-form label-width="80px" :model="datas" size="small">
      <!-- 标题内容 -->
      <el-form-item class="lef" label="选择模板">
        <p style="color: #000; line-height: 4px">{{ styleText }}</p>
      </el-form-item>
      <!-- 卡片样式选择 -->
      <div class="commodityType">
        <el-tooltip v-for="(item, index) in commodityTypes" :key="index" class="item" effect="dark" :content="item.content" placement="bottom">
          <span class="iconfont" style="font-size: 21px" :class="[item.type === datas.commodityType ? 'active' : '', item.icon]" @click="onTest(index)" />
        </el-tooltip>
      </div>
      <div class="bor" />
      <!-- 导航列表 -->
      <div>
        <vuedraggable v-model="datas.optionList" v-bind="dragOptions">
          <transition-group>
            <section v-for="(item, index) in datas.optionList" :key="item + index" class="txtList">
              <i class="el-icon-circle-close" @click="deleteOption(index)" />
              <!-- 标题和链接 -->
              <div class="opText">
                <el-input v-model="item.name" placeholder="请输入标题" size="mini" style="margin-bottom: 6px" clearable />
                <!-- 选择类型 -->
                <select-link @selectThisLink="selectThisLink" @clearThisLink="clearThisLink(index)" :linktypeurl="item.url" :theIndex="index" />

                <!-- 年级选择 -->
                <el-cascader :options="gradelist"  v-model="item.activeGrade" :props="{ multiple: true }" style="margin-top: 10px" @change="chooseGrade(index)" clearable></el-cascader>
                <el-select v-model="item.subjectCode" placeholder="请选择科目" style="margin-top: 6px">
                  <el-option
                    v-for="item in newSubjectArr"
                    :key="item.tagEnumCode"
                    :label="item.tagEnumName"
                    :value="item.tagEnumCode">
                  </el-option>
                </el-select>
              </div>
            </section>
          </transition-group>
        </vuedraggable>
      </div>

      <!-- 添加导航 -->
      <el-button type="primary" plain @click="addOption">
        <i class="el-icon-plus" />点击添加导航
      </el-button>
      <div class="bor" />
      <div style="height: 10px" />
      <!-- 样式 -->
      <el-form-item class="lef" label="导航样式">
        <div class="weiz">
          <el-tooltip v-for="index in 2" :key="index" effect="dark" :content="index - 1 === 0 ? '固定' : '滑动'"  placement="bottom">
            <i class="iconfont" :class="[index - 1 === 0 ? 'icon-guding' : 'icon-hengxianghuadong', datas.textStyle === index - 1 ? 'active' : '', ]" @click="datas.textStyle = index - 1" />
          </el-tooltip>
        </div>
      </el-form-item>

      <div style="height: 10px" />

      <!-- 文字行高 -->
      <el-form-item label="文字行高" class="lef">
        <el-slider v-model="datas.textHeight" :max="50" :min="24" input-size="mini" show-input />
      </el-form-item>

      <div style="height: 10px" />

      <!-- 文字大小 -->
      <el-form-item label="文字大小" prop="textSize" :hide-required-asterisk="true" class="lef">
        <el-input v-model.number="datas.textSize" type="number" placeholder="请输入文字大小" :maxlength="2" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import vuedraggable from "vuedraggable"; // 拖拽组件
import SelectLink from "../../../selectLink";
import { getTemplateTagLevel } from "@/api/basisMG";
export default {
  name: "TabControlstyle",
  components: { 
    vuedraggable, 
    SelectLink 
  },
  props: {
    datas: Object,
  },
  computed: {
    styleText() {
      if (this.datas.commodityType === 0) return "一级导航";
      if (this.datas.commodityType === 1) return "二级导航";
    }
  },
  data() {
    return {
      linktypeurl: "",
      currentIndex: 0,
      dragOptions: {
        // 拖拽配置
        animation: 200,
      },
      commodityTypes: [
        {
          icon: "icon-daohanghengxianghuadong",
          type: 0,
          content: "一级导航",
        },
        {
          icon: "icon-icon_tupiandaohang",
          type: 1,
          content: "二级导航",
        }
      ],
      props: { multiple: true },
      gradelist: [
        {
          value: "0001",
          label: "小学",
          children: [
            {
              value: "0001",
              label: "一年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
            {
              value: "0002",
              label: "二年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
            {
              value: "0003",
              label: "三年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
            {
              value: "0004",
              label: "四年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
            {
              value: "0005",
              label: "五年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
            {
              value: "0006",
              label: "六年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
          ],
        },
        {
          value: "0002",
          label: "初中",
          children: [
            {
              value: "0007",
              label: "七年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
            {
              value: "0008",
              label: "八年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
            {
              value: "0009",
              label: "九年级",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            },
          ],
        },
        {
          value: "0003",
          label: "高中",
          children: [
            {
              value: "0011",
              label: "高中",
              children:[
                {
                  value: "1", label: "含同步培优",
                },
                {
                  value: "2", label: "不含同步培优"
                }
              ]
            }
          ],
        },
      ],
      newSubjectArr: [],
      classListArr: []
    };
  },
  methods: {
    onTest(index) {
      this.datas.commodityType = index;
      this.$emit("updateView");
    },
    addOption() {
      this.datas.optionList.push({
        name: "",
        url: "",
        activeGrade: "", // 自用属性
        subjectCode: '',
        classList: [],
        bgcolor: "rgba(245, 245, 245, 1)",
        linktype: "",
      });
    },
    selectThisLink(resu) {
      this.datas.optionList.forEach((it, ind) => {
        if (ind == resu.index) {
          it.url = resu.url;
          it.linktype = resu.linktype;
        }
      });
    },
    clearThisLink (index) {
      this.datas.optionList[index].url = '';
    },
    deleteOption(index) {
      this.datas.optionList.splice(index, 1);
    },
    chooseGrade(index) {
      const valObj = this.datas.optionList[index].activeGrade.map((item) => ({
        stageCode: item[0],
        gradeCode: item[1],
        isTongbu: item[2]
      }));
      this.datas.optionList[index].classList = valObj;
      this.classListArr = valObj;
      this.getTemplateTagLevel();
    },
    //获取学段和科目
    async getTemplateTagLevel() {
      let arrSubje = [];
      const res = await getTemplateTagLevel({ id: "1361897479733286" });
      res.data.templateTagLevelDTOList.forEach((item) => {
        arrSubje.push(item.childrenDTOList);
      });
      this.subjectArr = JSON.parse(JSON.stringify(arrSubje));
      if (this.classListArr[0]) {
        this.chooseSubject(this.classListArr[0].stageCode);
      } else {
        this.datas.optionList.forEach(item => {
          this.chooseSubject(item.classList[0].stageCode)
        });
      }
    },
    chooseSubject(e) {
      // console.log(e,'eeeesubjectArr-----');
      if (e == "0001") {
        this.newSubjectArr = this.subjectArr[0];
      } else if (e == "0002") {
        this.newSubjectArr = this.subjectArr[1];
      } else if (e == "0003") {
        this.newSubjectArr = this.subjectArr[2];
      } else {
        this.newSubjectArr = this.subjectArr[0];
      }
    },
  },
  created () {
    if (!this.classListArr[0]) {
      this.getTemplateTagLevel();
    }
  }
};
</script>

<style scoped lang="scss">
.tabControlstyle {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 0 10px 20px;
  box-sizing: border-box;
  .bor {
    height: 1px;
    margin: 20px 0;
    background-color: #ebedf0;
  }
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }

  .lef {
    .el-form-item__label {
      text-align: left;
    }
  }
  .commodityType {
    display: flex;
    justify-content: space-around;
    align-items: center;
    span {
      display: inline-block;
      width: 58px;
      height: 32px;
      text-align: center;
      line-height: 32px;
      background: #fff;
      border: 1px solid #ebedf0;
      color: #979797;
      margin: 0 1px;
      cursor: pointer;
      transition: all 0.5s;

      &:hover {
        border: 1px solid #155bd4;
        color: #155bd4;
      }

      &.active {
        border: 1px solid #155bd4;
        background-color: #e0edff;
        color: #155bd4;
      }
    }
  }

  .txtList {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;

    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }

    /* 图片字 */
    .opText {
      width: 100%;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      justify-content: space-between;
      .select-type {
        display: flex;
        .el-select {
          .el-input {
            input {
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }
        }
      }
    }
  }

  /* 图片样式 */
  .weiz {
    text-align: right;
    i {
      padding: 5px 14px;
      margin-left: 10px;
      border-radius: 0;
      border: 1px solid #ebedf0;
      font-size: 20px;
      font-weight: 500;
      cursor: pointer;

      &:last-child {
        font-size: 22px;
      }

      &.active {
        color: #155bd4;
        border: 1px solid #155bd4;
        background: #e0edff;
      }
    }
  }

  .chooseys {
    padding: 10px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      font-size: 14px;
      color: #666;
    }
  }
}
</style>
